package com.boot.mgt.controller.web

import com.alibaba.fastjson.JSONObject
import com.boot.config.RequestWrapper
import com.boot.core.result.ResultTo
import com.boot.mgt.model.TaHmsPush
import com.boot.mgt.model.vo.TaPoliceSituationMonitoringVo
import com.boot.mgt.service.TaAccidentReportService
import com.boot.mgt.service.TaAccidentService
import com.boot.mgt.service.TaHmsPushService
import com.boot.mgt.service.TaPoliceSituationMonitoringService
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import javax.servlet.http.HttpServletRequest

/**
 * TaPoliceSituationMonitoringController - 指挥调度 - 警情监控
 */
@RestController
@RequestMapping("/admin/monitor")
class TaPoliceSituationMonitoringController {

    @Autowired
    private lateinit var taPoliceSituationMonitoringService: TaPoliceSituationMonitoringService;

    @Autowired
    private lateinit var taAccidentService: TaAccidentService

    @Autowired
    private lateinit var taAccidentReportService: TaAccidentReportService

    @Autowired
    private lateinit var taHmsPushService: TaHmsPushService

    /**
     * 警情监控 -- 数据累加
     *
     * 传入标识
     * id:                          id
     * regionId:                    管理部门Id
     * timeTag:                     %Y%m%d
     * casesAcceptedActionCurrent:  当前出警量(实时变化)
     * casesSceneDisposal:          现场处置量(实时变化)
     * casesAcceptedCalled:         接警量
     * casesAcceptedAction:         出警量
     * dealAccepted:                受理案件量
     * dealFastPay:                 快赔案件量
     * dealToNegotiate:             自行协商案件量
     * dealWechat:                  微信处理案件量
     * dealOthers:                  其他处理案件量
     * casesAcceptedOvertime:       超时未接警数量
     * dealOvertime:                超时未处理数量(去除)
     * casesReportOvertime:         超时未上报数量(去除)
     * casesOpenedOvertime:         超期未结案数量
     * casesRelatedOvertime:        超期未关联案件数量(指是否已关联六合一案件)
     * addTime:                     addTime
     * addBy:                       addBy
     * updateTime:                  updateTime
     * updateBy:                    updateBy
     */
    @PostMapping("/doMonitorAction")
    fun doMonitorAction(actionTagMap: Map<String, Any?>): Int? {
        return taPoliceSituationMonitoringService.doMonitorAction(actionTagMap);
    }

    /**
     * 警情监控 -- 今日/本月/本年统计(全部，包含年、月、日)
     * @param id: 主键ID
     * @param time_tag: 传入年月日字符串,
     *          格式: yyyyMMdd: 指定日期如 20200703
     *          格式: yyyyMM: 指定日期如 202007
     *          格式: yyyy: 指定日期如 2020
     */
//    @PostMapping("/getPoliceSituationMonitoring")
//    fun getPoliceSituationMonitoring(jsonStr: String): Map<String, TaPoliceSituationMonitoringVo?> {
//    @PostMapping("/getPoliceSituationMonitoring", produces= ["application/json;charset=UTF-8"])
//    fun getPoliceSituationMonitoring(@RequestBody jsonStr: String): Map<String, TaPoliceSituationMonitoringVo?> {
    @PostMapping("/getPoliceSituationMonitoring")
    fun getPoliceSituationMonitoring(request: HttpServletRequest): Map<String, TaPoliceSituationMonitoringVo?>? {
        var jsonStr: String = RequestWrapper(request).body
        var regionId = JSONObject.parseObject(jsonStr)["regionId"] as String?
        return taPoliceSituationMonitoringService.searchTaPoliceSituationMonitoringTimeRange(null, regionId);
    }

    /**
     * 警情监控 -- 今日/本月/本年统计
     * @param id: 主键ID
     * @param time_tag: 传入年月日字符串,
     *          格式: yyyyMMdd: 指定日期如 20200703
     *          格式: yyyyMM: 指定日期如 202007
     *          格式: yyyy: 指定日期如 2020
     */
    @PostMapping("/getPoliceSituationMonitoringDay")
    fun getPoliceSituationMonitoringDay(id: Long, time_tag: String, regionId: String): TaPoliceSituationMonitoringVo? {
        return taPoliceSituationMonitoringService.detail(if(StringUtils.isNotEmpty(id.toString())) id else null, if(StringUtils.isNotEmpty(time_tag)) time_tag else DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()), regionId);
    }

    /**
     * 警情监控 -- 今日/本月超时统计
     * @param id: 主键ID
     * @param time_tag: 传入年月日字符串,
     *          格式: yyyyMMdd: 指定日期如 20200703
     *          格式: yyyyMM: 指定日期如 202007
     *          格式: yyyy: 指定日期如 2020
     */
    @PostMapping("/getPoliceSituationMonitoringDayOvertime")
    fun getPoliceSituationMonitoringDayOvertime(id: Long, time_tag: String, regionId: String): TaPoliceSituationMonitoringVo? {
        return taPoliceSituationMonitoringService.detail(if(StringUtils.isNotEmpty(id.toString())) id else null, if(StringUtils.isNotEmpty(time_tag)) time_tag else DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()), regionId);
    }

    /**
     * 警情监控 -- 根据案件状态筛选案件信息
     * @param paramMap
     */
    @PostMapping("/getTaAccidentList")
    fun getTaAccidentList(paramMap: Map<String, Any?>): List<Map<String, Any?>>? {
        return taAccidentService.searchTaAccidentMapList(paramMap);
    }


    /**
     * 警情监控 - 根据案件ID返回案件详细信息, 受理案件显示现场采集信息
     */
    @GetMapping("/detail")
    fun getTaAccidentDetail(id: Int): ResultTo {
        return taAccidentReportService.detail(id, false)
    }

    /**
     * 更新实时动态坐标
     */
    @PostMapping("/doDynamicCoordinates")
    fun doDynamicCoordinates(id: String, longitude: Double, latitude: Double) {
        taAccidentService.updateDynamicCoordinates(id, longitude, latitude)
    }

    /**
     * 当前警力分布
     */
    @PostMapping("/getPoliceDistributionCurrent")
    fun getPoliceDistributionCurrent(request: HttpServletRequest): Any? {
        var result = JSONObject()
        var casesAcceptedActioningList = taAccidentReportService.searchCasesAcceptedActioningList()
        var casesSurveyingList = taAccidentReportService.searchCasesSurveyingList()
        result["casesAcceptedActioningList"] = casesAcceptedActioningList
        result["casesSurveyingList"] = casesSurveyingList
        return result
    }

    /**
     * 事故分布
     */
    @PostMapping("/getTrafficDistribution")
    fun getTrafficDistribution(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        return taAccidentReportService.searchTrafficDistributionList(map)
    }

    /**
     * 获取事故信息详情
     */
    @GetMapping("/getMqMsg")
    fun getMqMsg(police_number: String): Any? {
        var taHmsPushList: List<TaHmsPush>? = taHmsPushService.searchMqMessage(police_number)
        if (taHmsPushList != null) {
            return ResultTo().setData("taHmsPushList", taHmsPushList)
        }
        return ResultTo()
    }

    /**
     * 平均派警, 出警时间
     */
    @GetMapping("/getAverageActionTime")
    fun getAverageActionTime(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchAverageActionTime(map)
        return resultList
    }

    /**
     * 24小时警情统计
     * 接警数, 出警数
     */
    @GetMapping("/getStatisticsOfActionIn24H")
    fun getStatisticsOfActionIn24H(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchStatisticsOfActionIn24H(map)
        return resultList
    }

    /**
     * 警情分布
     * 接警数, 出警数
     */
    @GetMapping("/getDistributionOfPolice")
    fun getDistributionOfPolice(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchDistributionOfPolice(map)
        return resultList
    }

    /**
     * 处警分布
     * 接警数, 出警数
     */
    @GetMapping("/getDistributionOfPoliceAction")
    fun getDistributionOfPoliceAction(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchDistributionOfPoliceAction(map)
        return resultList
    }

    /**
     * 获取最近交通事故通知消息
     */
    @GetMapping("/getNoticeMessage")
    fun getNoticeMessage(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchNoticeMessage(map)
        return resultList
    }

    /**
     * 近期死亡事故概要
     */
    @GetMapping("/getRecentDeathSummary")
    fun getRecentDeathSummary(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchRecentDeathSummary(map)
        return resultList
    }

    /**
     * 今日亡人事故
     */
    @GetMapping("/getDeathAccidentToday")
    fun getDeathAccidentToday(request: HttpServletRequest): Any? {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        val resultList: List<Map<String, Any?>?>? = taPoliceSituationMonitoringService.searchDeathAccidentToday(map)
        return resultList
    }

}